<template>
    <el-container>
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="logo">
                <span>管理系统</span>
            </div>
            <el-button @click="logout" type="info">退出</el-button>
        </el-header>
        <!-- 主题区域 -->
            <el-container>
                <!-- 侧边栏区域 -->
                <el-aside :width="isCollpase ? '64px' : '200px'">
                  <div class="toggle-button" @click="toggleMenu">|||</div>
                    <!-- 菜单区域 -->
                    <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFE"
                    unique-opened :collapse="isCollpase" :collapse-transition="false" :router="true" :default-active="activePath">
                        <!-- 一级菜单 -->
                        <el-submenu :index="item.id+''" :key=item.id v-for="item in menuList">
                            <!-- 一级菜单模板区 -->
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-user-solid"></i>
                                <!-- 文本 -->
                                <span>{{ item.authName }}</span>
                            </template>

                            <!-- 二级菜单 -->
                            <el-menu-item :index=" '/' + subItem.path" :key="subItem.id" v-for="subItem in item.children"
                            @click="saveNavState('/' + subItem.path)">
                                <!-- 二级菜单模板区 -->
                                <template slot="title">
                                    <!-- 图标 -->
                                    <i class="el-icon-menu"></i>
                                    <!-- 文本 -->
                                    <span>{{subItem.authName}}</span>
                                </template>
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <!-- 主要内容区域 -->
                <el-main>
                  <!-- 占位符 -->
                  <router-view></router-view>
                </el-main>
            </el-container>
    </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 默认不折叠
      isCollpase: false,
      // 被保存激活的链接
      activePath: ''
    }
  },

  // 创建后显示左侧菜单函数
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  // 方法
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: ref } = await this.$http.get('/menus')
      if (ref.meta.status !== 200) return this.$message.error(ref.meta.msg)
      this.menuList = ref.data
    },
    // 点击按钮切换菜单的折叠和展开
    toggleMenu() {
      this.isCollpase = !this.isCollpase
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
// 整个容器
  .el-container {
    height: 100%;
  }

// 头部
  .el-header {
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    background-color: #373D41;
    > div {
        display: flex;
        align-items: center;

        span {
            margin-left: 15px;
        }
    }
  }

// 侧边栏
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none
    }
  }

// 内容
  .el-main {
    background-color: #ccc;
  }
// 菜单折叠
  .toggle-button {
    background-color: #415064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
